﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SharpDiary.Core.Utility;
using SharpDiary.Core.Controls;
using SharpDiary.Core.Security;
using Neotoma.Shell;
using SharpDiary.Diary;
using SharpDiary.Core.ORMapping;
using System.Threading;

namespace Neotoma
{
    static class Program
    {
        /// <summary>
        /// 是否显示 Splash 窗体。
        /// </summary>
        private static bool _showSplashWindow = true;
        private static readonly string SHARP_DIARY_MUTEX = "a3d6e212-1dee-47c4-a834-511732ede60f";

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();

            bool createdNew = false;
            Mutex mutex = new Mutex(false, SHARP_DIARY_MUTEX, out createdNew);

            if (createdNew)
            {
                Application.SetCompatibleTextRenderingDefault(false);

                if (_showSplashWindow)
                {
                    ShowSplashWindow();
                }

                SignIn();
            }
            else
            {
                MessageBoxHelper.ShowMessage("SharpDiary 已经运行。");
            }
        }

        /// <summary>
        /// 启动应用程序。
        /// </summary>
        private static void StartApplication(UserInfo userInfo)
        {
            frmShell shell = new frmShell(userInfo);
            UserSettings.ShellInstance = shell;
            shell.Size = new System.Drawing.Size(0, 0);

            Application.Run(shell);
        }

        /// <summary>
        /// 显示 Splash 窗体。
        /// </summary>
        private static void ShowSplashWindow()
        {
            using (frmSplash splashWindow = new frmSplash())
            {
                splashWindow.ShowDialog();
            }
        }

        /// <summary>
        /// 身份验证。
        /// </summary>
        private static void SignIn()
        {
            using (frmSignIn signInForm = new frmSignIn())
            {
                if (signInForm.ShowDialog() == DialogResult.OK)
                {
                    StartApplication(signInForm.CurrentUserInfo);
                }
            }
        }



    }
}